package y2016.date1021.pm.裁判评分;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

public class Grade {

    private static int grade[][] = new int[100][10];

    @Test
    public void main() throws Exception {
        makeGrade();
        outPutGrade();

    }

    /**
     * @param jp
     * @return 第一个最小，最后一个最大
     */
    private static int[] sort(int[] jp) {
        for (int j = 0; j < jp.length; j++) {

            for (int l = j + 1; l < jp.length; l++) {
                int temp = jp[l];

                if (jp[j] > jp[l]) {
                    jp[l] = jp[j];
                    jp[j] = temp;
                }
            }
        }
        System.out.println("评分排序：" + Arrays.toString(jp));
        return jp;
    }

    /**
     * 输出每张照片评分
     */
    public static void outPutGrade() {

        for (int i = 0; i < grade.length; i++) {
            int count = 0;
            int[] js = grade[i].clone();
            System.out.println("\n------第" + (i + 1) + "张照片------\n");
            //输出评分
            for (int j = 0; j < js.length; j++) {
                System.out.println("第" + (j + 1) + "号评委给分：" + grade[i][j]);
                count += js[j];
            }
            //排序求最高分和最低分
            int jp[] = sort(js);
            double ping = (double) (count - jp[0] - js[jp.length - 1]) / (jp.length - 2);//平均分
            System.out.println("照片最后得分：" + ping + "\n");

            //求平均分差
            int[] jg = grade[i];

            double[] abs = new double[jg.length];
            for (int j = 0; j < jg.length; j++) {
                int k = jg[j];
                abs[j] = Math.abs(ping - k);//绝对值
            }
            System.out.println("平均分差：" + Arrays.toString(abs));

            //标记评委号码，随着平均分差排序，以获取哪个评委最/失/公平
            int flags[] = new int[abs.length];
            for (int j = 0; j < flags.length; j++) {
                flags[j] = j;//顺序0123456789
            }
            //查找平均分差据最大的评委
            for (int j = 0; j < abs.length; j++) {
                for (int k = j + 1; k < abs.length; k++) {
                    double temp = abs[k];
                    int tf = flags[k];
                    if (abs[j] > temp) {
                        //分差交换
                        abs[k] = abs[j];
                        abs[j] = temp;
                        //评委号交换
                        flags[k] = flags[j];
                        flags[j] = tf;
                    }
                }
            }
            System.out.println("分差排序：" + Arrays.toString(abs));
            System.out.println("对应评委索引：" + Arrays.toString(flags));
            int gp = jg[flags[0]];//最公平的分数
            int bp = jg[flags[flags.length - 1]];//最不公平的分数

            System.out.println("\n最公平的分数：" + gp + "  最不公平的分数：" + bp);
            //分数有可能一样。所以需要遍历
            for (int j = 0; j < jg.length; j++) {
                int k = jg[j];
                if (k == gp) {
                    System.out.println("最公平的评委是" + (j + 1) + "号");
                }
                if (k == bp) {
                    System.out.println("最不公平的评委是" + (j + 1) + "号");
                }

            }
        }

    }

    /**
     * 生成评分
     */
    public static void makeGrade() {
        //外层for参赛照片
        for (int i = 0; i < grade.length; i++) {
            int[] js = grade[i];
            //评委
            for (int j = 0; j < js.length; j++) {
                int k = js[j];
                int fen = (int) (Math.random() * 100);//评分
                grade[i][j] = fen; //j号评委对i号照片打分
                System.out.println(fen);//打印评分过程
            }

        }

    }
}

